unit GetConf;
interface
  uses sysutils;
  type
   scif=set of char;
   Atargets=array[0..14] of boolean;
   Aterrain=record
    a,d,v:integer;
   end;
   Aunits=record
    a,d:integer;
   end;

   Runitcfg1=record
    name:string[15];
    iter:array[1..3] of byte;
    iterp:array[1..3] of byte;
    Atack:byte;
    Defence:byte;
    Move:byte;
    vision:byte;
    FuelAmmo:word;
    Cost:word;
    Targets:Atargets;
    Forest:Aterrain;
    Mount:Aterrain;
    Boloto:Aterrain;
    Town:Aterrain;
    Infantry:Aunits;
    Gun:Aunits;
    AirGun:Aunits;
    Tank:Aunits;
    Supplies:Aunits;
    Fighter:Aunits;
    Bomber:Aunits;
    Submarine:Aunits;
    Transport:Aunits;
    Tanker:Aunits;
    Cruiser:Aunits;
    BattleShip:Aunits;
    Destroyer:Aunits;
    Deminer:Aunits;
    Carrier:Aunits;
   end;

   Runitcfg=record
    name:string[15];
    iter:array[1..3] of byte;
    iterp:array[1..3] of byte;
    Atack:byte;
    Defence:byte;
    Move:byte;
    vision:byte;
    FuelAmmo:word;
    Cost:word;
    Targets:Atargets;
    Forest:Aterrain;
    Mount:Aterrain;
    Boloto:Aterrain;
    Desert:Aterrain;
    Arctic:Aterrain;
    Town:Aterrain;
    Infantry:Aunits;
    Gun:Aunits;
    AirGun:Aunits;
    Tank:Aunits;
    Supplies:Aunits;
    Fighter:Aunits;
    Bomber:Aunits;
    Submarine:Aunits;
    Transport:Aunits;
    Tanker:Aunits;
    Cruiser:Aunits;
    BattleShip:Aunits;
    Destroyer:Aunits;
    Deminer:Aunits;
    Carrier:Aunits;
   end;


   TunitCfg=Object
    count:integer;
    cfgs:array[0..14] of Runitcfg;
    Procedure LoadFromTextFile(name:string);
    Procedure SaveToFile(name:string);
    Procedure SaveToNewFile(name:string);
    Procedure LoadFromFile(name:string);
   end;

  var
   Hucfg:TunitCfg;

implementation

Procedure TUnitcfg.LoadFromFile;
var
 f:file;
 i:integer;
 len:word;
 it:word;
begin
 assignfile(f,name);reset(f,sizeof(cfgs));
 blockread(f,cfgs,1);
 closefile(f);
 for i:=0 to 14 do begin
  len:=cfgs[i].move;
  it:=len div 3;
  cfgs[i].iter[1]:=it;
  cfgs[i].iter[2]:=it;
  cfgs[i].iter[3]:=len-(it+it);
  if (cfgs[i].iter[3]-cfgs[i].iter[1])=2 then begin
   cfgs[i].iter[1]:=cfgs[i].iter[1]+1;
   cfgs[i].iter[3]:=cfgs[i].iter[3]-2;
   cfgs[i].iter[2]:=cfgs[i].iter[2]+1;
  end;
 end;
end;


Procedure Tunitcfg.LoadFromTextFile;
var
 f:Text;
 s:string;
 len,it,i,k:integer;
 cif:scif;

Function GetStr(st:string):string;
var
 i:integer;
 re:string;
 fl:Boolean;
begin
 re:='';fl:=false;
 for i:=1 to length(st) do begin
  if st[i]='=' then fl:=true;
  if (fl=true) and (st[i] <>'=') then re:=re+st[i];
 end;
 result:=re;
end;


Function Get2Coma(var ss:string):integer;
var
 i:integer;
 ts:string;
begin
 i:=0;
 repeat
  i:=i+1;
 until (ss[i]=',') or (i=length(ss));
 if ss[i]=',' then ts:=copy(ss,1,i-1) else ts:=copy(ss,1,i);
 result:=strtoint(ts);
 delete(ss,1,i);
end;


Function GetNum(st:string):integer;
var
i:integer;
 re:string;
 fl:Boolean;
begin
 cif:=['0','1','2','3','4','5','6','7','8','9'];
 re:='';fl:=false;
 for i:=1 to length(st) do begin
  if st[i]='=' then fl:=true;
  if (fl=true) and (st[i] in cif) then re:=re+st[i];
 end;
 if re='' then result:=0 else result:=strtoint(re);
end;

Function GetTargets(st:string):ATargets;
var
 i:integer;
 s:string;
 fl:boolean;
begin
  s:='';fl:=false;
  for i:=1 to length(st) do begin
   if st[i]='=' then fl:=true;
   if (fl=true) and (st[i]<>'=') then s:=s+st[i]
  end;
  for i:=1 to 15 do if s[i]='1' then result[i]:=true else result[i]:=false;
end;

Function GetTerra(st:string):Aterrain;
var
 i:integer;
 s:string;
 fl:boolean;
begin
  s:='';fl:=false;
  for i:=1 to length(st) do begin
   if st[i]='=' then fl:=true;
   if (fl=true) and (st[i]<>'=') then s:=s+st[i]
  end;
  result.a:=get2coma(s);
  result.d:=get2coma(s);
  result.v:=get2coma(s);
end;

Function GetUnit(st:string):Aunits;
var
 i:integer;
 s:string;
 fl:boolean;
begin
  s:='';fl:=false;
  for i:=1 to length(st) do begin
   if st[i]='=' then fl:=true;
   if (fl=true) and (st[i]<>'=') then s:=s+st[i]
  end;
  result.a:=get2coma(s);
  result.d:=get2coma(s);
end;

begin
 fillchar(cfgs,sizeof(cfgs),0);
 assignfile(f,name);
 reset(f);k:=-1;
 readln(f,s);
 repeat
   if s='' then else
   if s[1]='[' then begin
    k:=k+1;
    readln(f,s);cfgs[k].name:=getstr(s);
    readln(f,s);cfgs[k].atack:=getnum(s);
    readln(f,s);cfgs[k].Defence:=getnum(s);
    readln(f,s);cfgs[k].Move:=getnum(s);
    readln(f,s);cfgs[k].Vision:=getnum(s);
    readln(f,s);cfgs[k].FuelAmmo:=getnum(s);
    readln(f,s);cfgs[k].Cost:=getnum(s);
    readln(f,s);cfgs[k].Targets:=getTargets(s);
    readln(f,s);cfgs[k].Forest:=getTerra(s);
    readln(f,s);cfgs[k].mount:=getTerra(s);
    readln(f,s);cfgs[k].Boloto:=getTerra(s);
    readln(f,s);cfgs[k].Town:=getTerra(s);
    readln(f,s);cfgs[k].Infantry:=getUnit(s);
    readln(f,s);cfgs[k].Gun:=getUnit(s);
    readln(f,s);cfgs[k].AirGun:=getUnit(s);
    readln(f,s);cfgs[k].Tank:=getUnit(s);
    readln(f,s);cfgs[k].Supplies:=getUnit(s);
    readln(f,s);cfgs[k].Fighter:=getUnit(s);
    readln(f,s);cfgs[k].Bomber:=getUnit(s);
    readln(f,s);cfgs[k].Submarine:=getUnit(s);
    readln(f,s);cfgs[k].Transport:=getUnit(s);
    readln(f,s);cfgs[k].Tanker:=getUnit(s);
    readln(f,s);cfgs[k].Cruiser:=getUnit(s);
    readln(f,s);cfgs[k].BattleShip:=getUnit(s);
    readln(f,s);cfgs[k].Destroyer:=getUnit(s);
    readln(f,s);cfgs[k].DeMiner:=getUnit(s);
    readln(f,s);cfgs[k].Carrier:=getUnit(s);
   end;
   readln(f,s);
 until eof(f);
 closefile(f);
 for i:=0 to 14 do begin
  len:=cfgs[i].move;
  it:=len div 3;
  cfgs[i].iter[1]:=it;
  cfgs[i].iter[2]:=it;
  cfgs[i].iter[3]:=len-(it+it);
  if (cfgs[i].iter[3]-cfgs[i].iter[1])=2 then begin
   cfgs[i].iter[1]:=cfgs[i].iter[1]+1;
   cfgs[i].iter[3]:=cfgs[i].iter[3]-2;
   cfgs[i].iter[2]:=cfgs[i].iter[2]+1;
  end;
 end;
 SaveTofile('armies.dat');
end;

Procedure Tunitcfg.SaveToFile;
var
 f:File;
 ff:text;
 i:integer;
Begin
 assignfile(f,name);rewrite(f,sizeof(cfgs));
 blockwrite(f,cfgs,1);
 closefile(f);
end;

Procedure TunitCfg.savetonewfile;
var
 f:file of runitcfg1;
 r:Runitcfg;
 r1:Runitcfg1;
 i:integer;
begin

 assignfile(f,name);rewrite(f);
 for i:=0 to 14 do begin
  r:=cfgs[i];
    fillchar(r1,sizeof(runitcfg1),0);
    r1.name:=r.name;
    r1.Atack:=r.atack;
    r1.Defence:=r.Defence;
    r1.Move:=r.move;
    r1.vision:=r.vision;
    r1.FuelAmmo:=r.fuelammo;
    r1.Cost:=r.cost;
    r1.Targets:=r.targets;
    r1.Forest:=r.forest;
    r1.Mount:=r.mount;
    r1.Boloto:=r.boloto;
    r1.Town:=r.town;
    r1.Infantry:=r.infantry;
    r1.Gun:=r.gun;
    r1.AirGun:=r.airgun;
    r1.Tank:=r.tank;
    r1.Supplies:=r.supplies;
    r1.Fighter:=r.fighter;
    r1.Bomber:=r.bomber;
    r1.Submarine:=r.submarine;
    r1.Transport:=r.transport;
    r1.Tanker:=r.tanker;
    r1.Cruiser:=r.cruiser;
    r1.BattleShip:=r.battleship;
    r1.Destroyer:=r.destroyer;
    r1.Deminer:=r.deminer;
    r1.Carrier:=r.carrier;;
   write(f,r1);
  end;
  closefile(f);
end;
end.
